/**
 * Created by zhanlong on 2016/10/8.
 */
'use strict'
import {DeviceEventEmitter} from 'react-native';
// import UserEvents from '../events/UserEvents';
// import Toast from '../utils/ToastHelper';
// import LoadingOverlay from '../components/LoadingOverlay';

// import NetworkInfo from 'react-native-network-info';
// import UserManager from './UserManager';
const ISRELEASE: Boolean = false;//是否正式发布
const SERVER_PROJECT: string = "ruyipin";

/*----------------正式环境----------------*/
const PROTOCOL: string = "https";//上线
let HOST: String = "www.ruyipin.com";//上线
const PORT: string = "";//上线

/*----------------测试环境----------------*/
// const PROTOCOL: string = "http";//测试
// let HOST:String = "192.168.1.194:";//吴凯机器
// const PORT:string = "1503";

/*----------------debug环境----------------*/

// const PROTOCOL: string = "http";//debug
// let HOST: String = "120.132.23.214:";//debug
// const PORT: string = "1503";//debug

var urlPrefix: string = PROTOCOL + '://' + HOST + PORT + '/' + SERVER_PROJECT;
var urlSuffix: string = "";//"." + SERVER_TYPE;

/*-------------图片地址----------------------------*/
//路径设置 用于拼接url
// export const NET_HEADER:string = urlPrefix; //服务端协议路径
export const NET_ASSETS: string = PROTOCOL + '://' + HOST + PORT + "/ruyipin/app/getImage?path=images/"; //上线本地资源路径
// export const NET_ASSETS: string = 'http://' + '192.168.0.102:' + '8081' + "/share/images/"; //测试本地资源路径
// export const NET_ASSETS: string = PROTOCOL + '://' + HOST + PORT + "/images/"; //服务器资源路径

export const AVATAR_ASSETS: string = PROTOCOL + '://' + HOST + PORT + "/ruyipin/app/getImage?path=";//上线uclode
// export const AVATAR_ASSETS: string = PROTOCOL + '://' + HOST + PORT + "/"; //测试头像路径
// export const AVATAR_ASSETS:string = 'http://' + '192.168.0.101:' + '8081/share/'; //测试头像路径

// export const NET_DOCS_PATH:string = PROTOCOL + '://' + HOST + ':' + PORT + "/" + SERVER_USERPROT + "/" + SERVER_DOCS + "/";
// export const NET_DOMAIN:string = PROTOCOL + '://' + (ISRELEASE ? "www" : "test") + "." + DOMAIN;

// const DOMAIN:string = "ruyipin.com";
// const HOST_RELEASE:string = "www." + DOMAIN;//正式服
// const HOST_DEVELOPMENT:string = "test." + DOMAIN;//测试服
// let HOST: String = "192.168.0.101";//ISRELEASE ? HOST_RELEASE : HOST_DEVELOPMENT;
// const PORT: string = "8081";
// let HOST: String = "120.132.8.250";//测试服务器
// let HOST: String = "www.ruyipin.cn";
// const PORT: string = "";
// const SERVER_ROOT:string = "app";
// const SERVER_VERSION:string = "version";
// const SERVER_USER:string = "user";
// const SERVER_EVENT:string = "activity";
// const SERVER_MAIL:string = "mail";
// const SERVER_COMMON:string = "common";
// const SERVER_TYPE:string = "html";
// const SERVER_MONEY:string = "money";
// const SERVER_USERPROT:string = "userprot";
// const SERVER_DOCS:string = "docs";
/*
 * 请求服务器
 * @param url 要请求的服务器地址
 * @param callBack 请求成功后的回调函数
 * @param data 要传递的参数 为json对象 比如{参数名1:参数1,参数名2:参数2,...}
 * @param onRequestFail 请求失败后的回调函数 默认null
 * @param showLoading 是否显示loading效果 默认true
 * @param showAnimation 是否使用动画效果显示loading 默认true
 * @param shadow 是否显示半透明阴影 默认false
 */
class CustomRequest {
    _callBack: Function;

    constructor(url: string, callBack: Function, data: Object, onRequestFail: Function = null, showLoading: boolean = true, showAnimation: boolean = true, shadow: boolean = false) {
        this._callBack = callBack;
        var querystring = Object.keys(data).map(key => key + '=' + encodeURIComponent(data[key])).join('&');
        // console.log('++++++', querystring);
        if (showLoading) {
            // LoadingOverlay.setVisible(true, showAnimation, shadow);
        }
        ;
        let fetchObj = fetch(
            url,
            {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: querystring
            }
        );
        let fetchThen = fetchObj.then((response) => response.text());
        let fetchThenThen = fetchThen.then((responseText: string) => {
            if (showLoading) {
                // LoadingOverlay.setVisible(false);
            }
            ;
            var responseData: Object = JSON.parse(responseText);
            if (responseData.ret === -1) {
                this._callBack = null;
                // DeviceEventEmitter.emit(UserEvents.DID_LOGOUT);
                // 登录界面
                return;
            }
            if (responseData.hasOwnProperty('error')) {
                if (responseData.ret === -1) {
                    this._callBack = null;
                    // DeviceEventEmitter.emit(UserEvents.DID_LOGOUT);
                    // 登录界面
                    return;
                } else {
                    var errorStr: string = responseData.msg;
                    // var errorStr: string = responseData.msg;
                    // Toast.show(errorStr);
                }
                return;
            }
            if (this._callBack != null) this._callBack(responseData);
            this._callBack = null;
        });
        fetchThenThen.catch((error: string) => {
            if (showLoading)console.log("aaa","aa");
            // LoadingOverlay.setVisible(false);
                if (onRequestFail !== null) {
                    onRequestFail();
                }
            // console.warn("错误：" + error + ':' + url);
            // Toast.show(Languages.get('tip_request_fail'));
        });
    }
}

/*
 * 请求项目内服务器
 * @param serverType 要请求的协议 来自Protocol里的属性 比如PROTOCOL_LOGIN
 * @param responseBack 请求成功后的回调函数
 * @param data 要传递的参数 为json对象 比如{参数名1:参数1,参数名2:参数2,...}
 * @param onRequestFail 请求失败后的回调函数 默认null
 * @param showLoading 是否显示loading效果 默认true
 * @param showAnimation 是否使用动画效果显示loading 默认true
 * @param shadow 是否显示半透明阴影 默认false
 */
export function request(serverType: string, responseBack: Function, data: Object, onRequestFail: Function = null, showLoading: boolean = true, showAnimation: boolean = true, shadow: boolean = false) {
    // console.log("错误：" + urlPrefix + serverType + urlSuffix);
    new CustomRequest(urlPrefix + serverType + urlSuffix, responseBack, data, onRequestFail, showLoading, showAnimation, shadow);

};
/*
 * 根据url地址请求任意服务器
 * @param url 要请求的服务器地址 比如http://www.baidu.com
 * @param responseBack 请求成功后的回调函数
 * @param data 要传递的参数 为json对象 比如{参数名1:参数1,参数名2:参数2,...}
 * @param onRequestFail 请求失败后的回调函数 默认null
 * @param showLoading 是否显示loading效果 默认true
 * @param showAnimation 是否使用动画效果显示loading 默认true
 * @param shadow 是否显示半透明阴影 默认false
 */
export function requestURL(url: string, responseBack: Function, data: Object, onRequestFail: Function = null, showLoading: boolean = false, showAnimation: boolean = false, shadow: boolean = false) {
    new CustomRequest(url, responseBack, data, onRequestFail, showLoading, showAnimation, shadow);
};
